<template>
  <el-dialog :model-value="dialogVisible" :title="state.title" width="40%" align-center @closed="handleCancel">
    <el-form ref="refForm" :model="form.data" :rules="form.rules" label-position="right" label-width="80px">
      <el-form-item prop="parentId" label="上级部门">
        <el-tree-select
          style="width: 100%"
          placeholder="请选择上级部门"
          v-model="form.data.parentId"
          :props="treeProps"
          :data="parentData"
          :render-after-expand="false"
          check-strictly
        />
      </el-form-item>
      <el-row>
        <el-col :span="12">
          <el-form-item prop="deptName" label="部门名称">
            <el-input v-model="form.data.deptName" placeholder="请输入部门名称"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item prop="deptCode" label="部门编码">
            <el-input v-model="form.data.deptCode" placeholder="请输入部门编码"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item prop="telephone" label="电话">
            <el-input v-model="form.data.telephone" placeholder="请输入电话"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item prop="seq" label="顺序">
            <el-input-number v-model="form.data.seq" :min="1" placeholder="请输入顺序"></el-input-number>
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item prop="status" label="状态">
        <el-radio-group v-model="form.data.status">
          <el-radio :label="1">启用</el-radio>
          <el-radio :label="0">禁用</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleSave">保 存</el-button>
        <el-button @click="handleCancel">取 消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script setup lang="ts">
import { withDefaults, computed, reactive, ref, nextTick } from 'vue'
import {
  ElDialog,
  ElMessage,
  ElForm,
  ElFormItem,
  ElInput,
  ElRadioGroup,
  ElRadio,
  ElTreeSelect,
  ElInputNumber
} from 'element-plus'
import { util } from '@/utils'
import { deptApi } from '@/api/modules/system/dept'
import type { Dept } from '@/api/modules/system/dept'

const props = withDefaults(defineProps<{ visible: boolean; parentData: Dept[] }>(), {
  visible: false,
  parentData: () => [] as Dept[]
})
const treeProps = {
  value: 'deptId',
  label: 'deptName'
}

const parentData = computed(() => {
  const data = [{ deptId: '', deptName: '<空>' }]
  return data.concat(props.parentData)
})

const emits = defineEmits(['success', 'update:visible'])

const dialogVisible = computed(() => props.visible)

const refForm = ref()
const form = reactive<{ data: Dept; rules: any }>({
  data: { status: 1 } as Dept,
  rules: {
    deptName: [{ required: true, message: '请输入部门名称' }]
  }
})

const state = computed(() => {
  if (form.data.deptId) {
    return { isCreate: false, title: '修改部门' }
  } else {
    return { isCreate: true, title: '新建部门' }
  }
})

const handleSave = () => {
  refForm.value.validate((valid: boolean) => {
    if (valid) {
      saveCallback(deptApi.save(form.data))
    }
  })
}
const saveCallback = (promise: Promise<AxiosType.Response>) => {
  promise.then(resp => {
    if (resp.success) {
      ElMessage.success(`已${state.value.title} [ ${form.data.deptName} ]`)
      emits('success')
      handleCancel()
    } else {
      ElMessage.error(resp.message)
    }
  })
}

const handleCancel = () => {
  form.data = {} as Dept
  refForm.value.resetFields()
  emits('update:visible', false)
}

const setFormData = (data: Dept) => {
  nextTick(() => (form.data = util.cloneObject(data)))
}
defineExpose({ setFormData })
</script>

<style lang="scss" scoped></style>
